package com.sairobo.live.util;

import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import java.util.List;

/**
 * 针对路径操作常用工具类
 * <p>
 * Created by sdevil507 on 2017/4/20.
 */
public class UrlUtil {

    /**
     * 利用AntPathMatcher校验输入url是否匹配,匹配单个规则<br>
     *
     * @param sourceUrl String类型Url
     * @param pattern   antPath类型规则:["/user","/user/*","/user/**"等]
     * @return boolean
     */
    public static boolean urlAntPathMatch(String sourceUrl, String pattern) {
        PathMatcher matcher = new AntPathMatcher();
        boolean result = matcher.match(pattern, sourceUrl);
        return result;
    }

    /**
     * 利用AntPathMatcher校验输入url是否匹配,匹配规则列表<br>
     *
     * @param sourceUrl String类型Url
     * @param patterns  antPath类型list规则:["/user","/user/*","/user/**"等]
     * @return boolean
     */
    public static boolean urlAntPathMatchForPatterns(String sourceUrl, List<String> patterns) {
        for (String pattern : patterns) {
            if (urlAntPathMatch(sourceUrl, pattern)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 利用AntPathMatcher校验输入url是否匹配,匹配规则列表<br>
     *
     * @param sourceUrl String类型Url
     * @param patterns  antPath类型list规则:["/user","/user/*","/user/**"等]
     * @return boolean
     */
    public static boolean urlAntPathMatchForPatterns(String sourceUrl, String[] patterns) {
        for (String pattern : patterns) {
            if (urlAntPathMatch(sourceUrl, pattern)) {
                return true;
            }
        }
        return false;
    }
}
